<template>
  <!--设备列表-新增设备-->
  <j-modal :title="title" :width="width" :visible="visible" switchFullscreen @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    :cancelButtonProps="{ class:{'jee-hidden': disableSubmit} }" @cancel="handleCancel" cancelText="关闭" okText="确定">
    <!--表单-->
    <a-spin :spinning="confirmLoading">
      <j-form-container class="scroll-view" style="height: 600px">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="12">
              <a-form-model-item label="归属项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName"
                style="margin-bottom: 10px;">
                <a-select :disabled="disabled" placeholder="请选择归属项目" :value="model.projectName"
                  @change="handleChange('project',$event)">
                  <a-select-option v-for="(item,index) in projectList" :key="index" :value="item">
                    {{item.label}}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeName"
                style="margin-bottom: 10px;">
                <div style="display: flex;align-items: center;">
                  <a-select :disabled="true" placeholder="请选择分类" :value="model.categoryOneName"
                    @change="handleChange('categoryOne',$event)" style="margin-right: 10px;width: 174px;">
                    <a-select-option v-for="(item,index) in categoryOneList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                  <a-select :disabled="disabled" placeholder="请选择设备类型" :value="model.typeName"
                    @change="handleChange('type',$event)" style="width: 174px;">
                    <a-select-option v-for="(item,index) in typeList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                </div>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="生产厂家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="factory"
                style="margin-bottom: 10px;">
                <a-input :disabled="disabled" v-model="model.factory" placeholder="请输入生产厂家"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name"
                style="margin-bottom: 10px;">
                <a-input :disabled="disabled" v-model="model.name" placeholder="请输入设备名称"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="model"
                style="margin-bottom: 10px;">
                <a-input :disabled="disabled" v-model="model.model" placeholder="请输入规格型号"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="设备编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number"
                style="margin-bottom: 10px;">
                <a-input :disabled="disabled" v-model="model.number" placeholder="请输入设备编号"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="制造年月" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="makeDate"
                style="margin-bottom: 10px;">
                <!-- <a-input :disabled="disabled" v-model="model.makeDate" placeholder="请输入制造年月"></a-input> -->
                <j-date :disabled="disabled" v-model="model.makeDate" :showTime="true" date-format="YYYY-MM" style=""
                  placeholder="请选择制造年月" style="width: 100%;"></j-date>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="功率（kw）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="power"
                style="margin-bottom: 10px;">
                <a-input :disabled="disabled" v-model="model.power" placeholder="请输入功率"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="重量（kg）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weight"
                style="margin-bottom: 10px;">
                <a-input :disabled="disabled" v-model="model.weight" placeholder="请输入重量"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierName"
                style="margin-bottom: 10px;">
                <a-select :disabled="disabled" placeholder="请选择供应商" :value="model.supplierName"
                  @change="handleChange('supplier',$event)">
                  <a-select-option v-for="(item,index) in supplierList" :key="index" :value="item">
                    {{item.label}}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="24">
              <a-form-model-item label="设备位置" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="address"
                style="margin-bottom: 10px;">
                <div style="display: flex;align-items: center;">
                  <a-select :disabled="disabled" placeholder="请选择项目" :value="model.addProjectName"
                    @change="handleChange('addProject',$event)" style="margin-right: 10px;width: 283px;">
                    <a-select-option v-for="(item,index) in projectList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                  <a-select :disabled="disabled || model.addProjectId ===''" placeholder="请选择设备位置"
                    :value="model.addDepartName" @change="handleChange('addDepart',$event)"
                    style="margin-right: 10px;width: 283px;">
                    <a-select-option v-for="(item,index) in addDepartList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                  <a-select :disabled="disabled || model.addDepartId ===''" placeholder="请选择设备位置"
                    :value="model.address" @change="handleChange('address',$event)" style="width: 283px;">
                    <a-select-option v-for="(item,index) in addressList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                </div>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="采购日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseDate"
                style="margin-bottom: 10px;">
                <j-date :disabled="disabled" v-model="model.purchaseDate" :showTime="true" date-format="YYYY-MM-DD"
                  style="" placeholder="请选择采购日期" style="width: 100%;"></j-date>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="进场时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approachTime"
                style="margin-bottom: 10px;">
                <j-date :disabled="disabled" v-model="model.approachTime" :showTime="true" date-format="YYYY-MM-DD "
                  style="" placeholder="请选择进场时间" style="width: 100%;"></j-date>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone"
                style="margin-bottom: 10px;">
                <a-input :disabled="disabled" v-model="model.phone" placeholder="请输入联系电话"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="statusName"
                style="margin-bottom: 10px;">
                <a-select :disabled="disabled" placeholder="请选择设备状态" :value="model.statusName"
                  @change="handleChange('status',$event)">
                  <a-select-option v-for="(item,index) in statusList" :key="index" :value="item">
                    {{item.label}}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="保修期限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="termService"
                style="margin-bottom: 10px;">
                <!-- <a-select :disabled="disabled" placeholder="请选择保修期限" :value="model.termServiceName"
                  @change="handleChange('termService',$event)">
                  <a-select-option v-for="(item,index) in termServiceList" :key="index" :value="item">
                    {{item.label}}
                  </a-select-option>
                </a-select> -->
                <a-input :disabled="disabled" v-model="model.termService" placeholder="请输入保修期限"
                  style="width: calc(100% - 30px);margin-right: 10px;"></a-input>年
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="使用状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stateName"
                style="margin-bottom: 10px;">
                <a-select :disabled="disabled" placeholder="请选择使用状态" :value="model.stateName"
                  @change="handleChange('state',$event)">
                  <a-select-option v-for="(item,index) in stateList" :key="index" :value="item">
                    {{item.label}}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="保管人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departName"
                style="margin-bottom: 10px;">
                <div style="display: flex;align-items: center;">
                  <a-select :disabled="disabled" placeholder="请选择项目" :value="model.preserverProjectName"
                    @change="handleChange('preserverProject',$event)" style="margin-right: 10px;width: 112px;">
                    <a-select-option v-for="(item,index) in projectList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                  <a-select :disabled="disabled || model.preserverProjectId ===''" placeholder="请选择部门"
                    :value="model.departName" @change="handleChange('depart',$event)"
                    style="margin-right: 10px;width: 112px;">
                    <a-select-option v-for="(item,index) in departList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                  <a-select :disabled="disabled || model.departId ===''" placeholder="请选择人员" :value="model.userName"
                    @change="handleChange('user',$event)" style="width: 112px;">
                    <a-select-option v-for="(item,index) in userList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                </div>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="牌号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mark"
                style="margin-bottom: 10px;">
                <a-input :disabled="disabled" v-model="model.mark" placeholder="请输入牌号"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="估值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appraisement"
                style="margin-bottom: 10px;">
                <a-input :disabled="disabled" v-model="model.appraisement" placeholder="请输入估值"
                  style="width: calc(100% - 30px);margin-right: 10px;"></a-input>元
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="来源类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceTypeName"
                style="margin-bottom: 10px;">
                <a-select :disabled="disabled" placeholder="请选择来源类型" :value="model.sourceTypeName"
                  @change="handleChange('sourceType',$event)">
                  <a-select-option v-for="(item,index) in sourceTypeList" :key="index" :value="item">
                    {{item.label}}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="班组" :labelCol="labelCol" v-if="model.sourceType != '1'" :wrapperCol="wrapperCol"
                prop="teamGroupName" style="margin-bottom: 10px;">
                <a-select :disabled="disabled" placeholder="请选择班组" :value="model.teamGroupName"
                  @change="handleChange('teamGroup',$event)">
                  <a-select-option v-for="(item,index) in teamGroupList" :key="index" :value="item">
                    {{item.label}}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="8">
              <a-form-model-item label="是否大型设备" :labelCol="labelCol3" :wrapperCol="wrapperCol3"
                prop="isLargeScaleEquipment" style="margin-bottom: 10px;">
                <a-radio-group :disabled="disabled" :options="isLargeScaleEquipment"
                  :default-value="model.isLargeScaleEquipment" @change="onChange('isLargeScaleEquipment',$event)"
                  style="margin-left: 10px;" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="安全装置是否有效" :labelCol="labelCol3" :wrapperCol="wrapperCol3" prop="isSafetyDevice"
                style="margin-bottom: 10px;">
                <a-radio-group :disabled="disabled" :options="isSafetyDevice" :default-value="model.isSafetyDevice"
                  @change="onChange('isSafetyDevice',$event)" style="margin-left: 10px;" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="是否特种设备" :labelCol="labelCol3" :wrapperCol="wrapperCol3"
                prop="isSpecialEquipment" style="margin-bottom: 10px;">
                <a-radio-group :disabled="disabled" :options="isSpecialEquipment"
                  :default-value="model.isSpecialEquipment" @change="onChange('isSpecialEquipment',$event)"
                  style="margin-left: 10px;" />
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="安全装置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="safetyDevice"
                style="margin-bottom: 10px;">
                <textarea :disabled="disabled" v-model="model.safetyDevice" cols="100" rows="3"
                  style="width: 100%;padding:0 30px 0 10px;line-height: 25px;" maxlength="100"
                  placeholder="请输入安全装置"></textarea>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="附属装置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accessoryDevice"
                style="margin-bottom: 10px;">
                <textarea :disabled="disabled" v-model="model.accessoryDevice" cols="100" rows="3"
                  style="width: 100%;padding:0 30px 0 10px;line-height: 25px;" maxlength="100"
                  placeholder="请输入附属装置"></textarea>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="24">
              <a-form-model-item label="证件上传" :labelCol="labelCol2" :wrapperCol="wrapperCol2"
                prop="specialEquipmentOperationUrl" style="margin-bottom: 10px;">
                <div style="display: flex;align-items: center;margin-right: 50px;">
                  <div style="flex: 1;margin-right: 20px;">
                    <j-upload text="特种设备操作证" v-model="model.specialEquipmentOperationUrl" :disabled="disabled"
                      :buttonVisible="!disabled"></j-upload>
                  </div>
                  <div style="flex: 1;margin-right: 20px;">
                    <j-upload text="特种设备证" v-model="model.specialEquipmentUrl" :disabled="disabled"
                      :buttonVisible="!disabled"></j-upload>
                  </div>
                  <div style="flex: 1;">
                    <j-upload text="设备环保标志" v-model="model.ecoMarkUrl" :disabled="disabled"
                      :buttonVisible="!disabled"></j-upload>
                  </div>
                </div>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="24">
              <a-form-model-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
                style="margin-bottom: 10px;">
                <div style="position: relative;">
                  <textarea :disabled="disabled" v-model="model.remark" cols="100" rows="3"
                    style="width: 95%;padding:0 30px 0 10px;line-height: 25px;" maxlength="500"
                    placeholder="请输入备注"></textarea>
                  <div style="position: absolute;right: 64px;bottom: 20px;font-size: 12px;">{{model.remark.length}}/500
                  </div>
                </div>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="24">
              <a-form-model-item label="设备图片" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="picture"
                style="margin-bottom: 10px;">
                <j-upload v-model="model.picture" fileType="image" :disabled="disabled" :buttonVisible="!disabled"
                  text="上传设备图片"></j-upload>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
  </j-modal>
</template>

<script>
  import {
    httpAction,
    getAction,
    postAction
  } from '@/api/manage'
  export default {
    name: 'WtDevicetypeModal',
    props: {
      pageType: {
        type: String,
        default: '', //add-新增 anew-重复提交 edit-编辑 detail-详情 approver-审批
      },
    },
    data() {
      return {
        title: '',
        width: 1100,
        visible: false,
        disableSubmit: false,
        //表单
        confirmLoading: false,
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 6
          }
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          }
        },
        labelCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 3
          }
        },
        wrapperCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 21
          }
        },
        labelCol3: {
          xs: {
            span: 24
          },
          sm: {
            span: 12
          }
        },
        wrapperCol3: {
          xs: {
            span: 24
          },
          sm: {
            span: 12
          }
        },
        validatorRules: {},
        model: {
          id:'',
          projectName: '',
          projectId: '',
          categoryOneName: '',
          categoryOneId: '',
          typeName: '',
          type: '',
          name: '',
          model: '',
          factory: '',
          power: '',
          makeDate: '',
          weight: '',
          addProjectName: '',
          addProjectId: '',
          addDepartName: '',
          addDepartId: '',
          address: '',
          teamGroupName: '',
          teamGroup: '',
          supplierName: '',
          supplier: '',
          approachTime: '',
          phone: '',
          sourceTypeName: '',
          sourceType: '',
          purchaseDate: '',
          statusName: '',
          status: '',
          termService: '',
          stateName: '',
          state: '',
          preserverProjectName: '',
          preserverProjectId: '',
          departName: '',
          departId: '',
          userName: '',
          userId: '',
          mark: '',
          appraisement: '',
          isLargeScaleEquipment: '0',
          isSafetyDevice: '0',
          isSpecialEquipment: '0',
          safetyDevice: '',
          accessoryDevice: '',
          specialEquipmentOperationUrl: '',
          specialEquipmentUrl: '',
          ecoMarkUrl: '',
          remark: '',
          picture: '',
          number: '',
        },
        projectList: [], //项目列表
        categoryOneList: [], //
        typeList: [], //设备类型
        addressList: [], //设备位置列表
        addDepartList: [], //设备位置-部门
        teamGroupList: [], //班组列表
        supplierList: [], //供应商列表
        sourceTypeList: [{
          label: '自有',
          value: '1'
        }, {
          label: '月租',
          value: '2'
        }, {
          label: '台班',
          value: '3'
        }, {
          label: '班组',
          value: '4'
        }], //来源类型
        statusList: [{
          label: '较差',
          value: '1'
        }, {
          label: '一般',
          value: '2'
        }, {
          label: ' 良好',
          value: '3'
        }], //设备状态
        termServiceList: [{
          label: '1年',
          value: '1年'
        }, {
          label: '2年',
          value: '2年'
        }, {
          label: '3年',
          value: '3年'
        }, {
          label: '5年',
          value: '5年'
        }, {
          label: '10年',
          value: '10年'
        }], //保修期限
        stateList: [{
          label: '闲置',
          value: '1'
        }, {
          label: '待修',
          value: '2'
        }, {
          label: ' 在用',
          value: '3'
        }], //使用状态
        departList: [], //部门列表
        userList: [], //保管人姓名列表
        isLargeScaleEquipment: [{
          label: '是',
          value: '1'
        }, {
          label: '否',
          value: '0'
        }], //是否大型设备
        isSafetyDevice: [{
          label: '是',
          value: '1'
        }, {
          label: '否',
          value: '0'
        }], //是否安全装置是否有效
        isSpecialEquipment: [{
          label: '是',
          value: '1'
        }, {
          label: '否',
          value: '0'
        }], //是否特种设备
        url: {
          add: '/equipment/wtEquipment/add',
          edit: '/equipment/wtEquipment/edit',
          list: '/equipment/wtEquipment/list',
          detail: '/equipment/wtEquipment/queryById',
          delete: '',
        },
        disabled: false,
      }
    },
    created() {

    },
    mounted() {

    },
    methods: {
      onChange(type, item, ) {
        console.log(1, type, item);
        if (type === 'isLargeScaleEquipment') {
          this.model.isLargeScaleEquipment = item.target.value;
        } else if (type === 'isSafetyDevice') {
          this.model.isSafetyDevice = item.target.value;
        } else if (type === 'isSpecialEquipment') {
          this.model.isSpecialEquipment = item.target.value;
        }
      },
      //下拉选择
      handleChange(type, item, index) {
        console.log(1, type, item, index);
        let _self = this;
        if (type === 'project') { //归属项目/设备位置/保管人
          this.model.projectName = item.label;
          this.model.projectId = item.value;
          //清空联动数据
          this.model.departName = '';
          this.model.departId = '';
          this.model.userName = '';
          this.model.userId = '';
          this.userList = [];
          getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.projectId}`).then((res) => {
            if (res.success) {
              _self.departList = [];
              let treeList = res.result;
              _self.$nextTick(() => {
                _self.departList = [...treeList].map((v) => ({
                  ...v,
                  value: v.id,
                  label: v.departName,
                }))
                _self.$forceUpdate();
              })
            }
          })
        } else if (type === 'addProject') { //设备位置-项目
          this.model.addProjectName = item.label;
          this.model.addProjectId = item.value;
          //清空联动数据
          this.model.addDepartName = '';
          this.model.addDepartId = '';
          this.model.address = '';
          this.addressList = [];
          getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.addProjectId}`)
            .then((res) => {
              if (res.success) {
                _self.addDepartList = [];
                let treeList = res.result;
                _self.$nextTick(() => {
                  _self.addDepartList = [...treeList].map((v) => {
                    return {
                      ...v,
                      value: v.id,
                      label: v.departName,
                    }
                  })
                  _self.$forceUpdate();
                })
              }
            })
        } else if (type === 'addDepart') { //设备位置-部门
          this.model.addDepartName = item.label;
          this.model.addDepartId = item.value;
          //清空联动数据
          this.model.address = '';
          getAction(`/equipment/wtEquipmentAddress/getWtEquipmentAddressList?departId=${this.model.addDepartId}&projectId=&address=`).then((res) => {
              if (res.success) {
                this.addressList = [];
                let treeList = res.result;
               this.addressList = [...treeList].map((v) => ({
                 ...v,
                 value: v.id,
                 label: v.address,
               }))
               this.$forceUpdate();
              }
            })
        } else if (type === 'address') { // 设备位置-详细位置
          this.model.address = item.label;
        } else if (type === 'type') { //设备类型
          this.model.typeName = item.label;
          this.model.type = item.value;
        } else if (type === 'preserverProject') { //保管人-项目
          this.model.preserverProjectName = item.label;
          this.model.preserverProjectId = item.value;
          //清空联动数据
          this.model.departName = '';
          this.model.departId = '';
          this.model.userName = '';
          this.model.userId = '';
          getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.preserverProjectId}`)
            .then((res) => {
              if (res.success) {
                _self.departList = [];
                let treeList = res.result;
               _self.departList = [...treeList].map((v) => ({
                 ...v,
                 value: v.id,
                 label: v.departName,
               }))
               _self.$forceUpdate();
              }
            })
        } else if (type === 'depart') { //保管人-部门
          this.model.departName = item.label;
          this.model.departId = item.value;
          //清空联动数据
          this.model.userName = '';
          this.model.userId = '';
          getAction(`/sys/sysDepart/getUsersByDepartId?id=${this.model.departId}`).then((res) => {
            if (res.success) {
              _self.userList = [];
              let treeList = res.result;
              _self.userList = [...treeList].map((v) => ({
                ...v,
                value: v.id,
                label: v.realname,
              }))
              _self.$forceUpdate();
            }
          })
        } else if (type === 'user') { //保管人-人员
          this.model.userName = item.label;
          this.model.userId = item.value;
        } else if (type === 'teamGroup') {
          this.model.teamGroupName = item.label;
          this.model.teamGroup = item.value;
        } else if (type === 'supplier') {
          this.model.supplierName = item.label;
          this.model.supplier = item.value;
        } else if (type === 'sourceType') {
          this.model.sourceTypeName = item.label;
          this.model.sourceType = item.value;
        } else if (type === 'status') {
          this.model.statusName = item.label;
          this.model.status = item.value;
        } else if (type === 'state') {
          this.model.stateName = item.label;
          this.model.state = item.value;
        } else if (type === 'termService') {
          //this.model.termServiceName = item.label;
          //this.model.termService = item.value;
        }
        _self.$forceUpdate();
      },
      //获取数据字典列表
      Dictionary() {
        var _self = this;
        //项目列表
        getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
          if (res.success) {
            this.projectList = [];
            let treeList = res.result;
            this.projectList = [...treeList].map((v) => ({
              ...v,
              value: v.id,
              label: v.departName,
            }))
            if (this.pageType === 'edit' || this.pageType === 'detail') {
              if (this.projectList && this.projectList.length > 0) {
                let info1 = this.projectList.find((item) => item.value == this.model.projectId);
                if (!!info1) {
                  this.model.projectName = info1.label;
                }
                let info2= this.projectList.find((item) => item.value == this.model.preserverProjectId);
                if (!!info2) {
                  this.model.preserverProjectName = info2.label;
                }
                let info3 = this.projectList.find((item) => item.value == this.model.addProjectId);
                if (!!info3) {
                  this.model.addProjectName = info3.label;
                }
                
              }
              getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.preserverProjectId}`)
                .then((res) => {
                  if (res.success) {
                    this.departList = [];
                    let treeList = res.result;
                    this.departList = [...treeList].map((v) => ({
                      ...v,
                      value: v.id,
                      label: v.departName,
                    }))
                    if (this.departList && this.departList.length > 0) {
                      let info4 = this.departList.find((item) => item.value == this.model.departId);
                      if (!!info4) {
                        this.model.departName = info4.label;
                      }
                    }
                  }
                })
              getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.addProjectId}`)
                .then((res) => {
                  if (res.success) {
                    this.addDepartList = [];
                    let treeList = res.result;
                    this.addDepartList = [...treeList].map((v) => ({
                      ...v,
                      value: v.id,
                      label: v.departName,
                    }))
                    if (this.addDepartList && this.addDepartList.length > 0) {
                      let info4 = this.addDepartList.find((item) => item.value == this.model.addDepartId);
                      if (!!info4) {
                        this.model.addDepartName = info4.label;
                      }
                    }
                  }
                })
            }
          }
        })

        //设备类型
        getAction('/wtCategory/list').then((res) => {
          if (res.success) {
            let treeList1 = res.result;
            let treeList = [];
            if (treeList1 && treeList1.length > 0) {
              treeList1.forEach(ite => {
                if (ite.name === '设备机械') {
                  this.model.categoryOneId = ite.id;
                  this.model.categoryOneName = ite.name;
                  let categoryOneList = [];
                  categoryOneList.push(ite);
                  this.categoryOneList = categoryOneList.map(v => ({
                    ...v,
                    value: v.id,
                    label: v.name,
                  }));
                  if (ite.children && ite.children.length > 0) {
                    treeList = ite.children;
                  }
                }
              })
              if (treeList && treeList.length > 0) {
                this.typeList = [...treeList].map((v) => ({
                  ...v,
                  value: v.id,
                  label: v.name,
                }))
              }
            }
            if (this.pageType === 'edit' || this.pageType === 'detail') {
              let info = this.typeList.find((item) => item.value == this.model.type);
              if (!!info) {
                this.model.typeName = info.label;
              }
            }
          }
        })

        //设备位置列表
        getAction(`/equipment/wtEquipmentAddress/list`).then((res2) => {
          if (res2.success) {
            let treeList = res2.result.records
            if (treeList && treeList.length > 0) {
              this.addressList = [...treeList].map((v) => ({
                ...v,
                value: v.id,
                label: v.address,
              }))
            }
          }
        })

        //班组列表
        getAction(`/wtSupplier/list?type=1`).then((res2) => {
          if (res2.success) {
            let treeList = res2.result.records
            if (treeList && treeList.length > 0) {
              this.teamGroupList = [...treeList].map((v) => ({
                ...v,
                value: v.id,
                label: v.name,
              }))
            }
            if (this.pageType === 'edit' || this.pageType === 'detail') {
              let info = this.teamGroupList.find((item) => item.value == this.model.teamGroup);
              if (!!info) {
                this.model.teamGroupName = info.label;
              }
            }
          }
        })

        //供应商列表
        getAction(`/wtSupplier/list?type=0`).then((res) => {
          if (res.success) {
            let treeList = res.result.records;
            if (treeList && treeList.length > 0) {
              this.supplierList = [...treeList].map((v) => ({
                ...v,
                value: v.id,
                label: v.name,
              }))
            }
            if (this.pageType === 'edit' || this.pageType === 'detail') {
              let info = this.supplierList.find((item) => item.value == this.model.supplier);
              if (!!info) {
                this.model.supplierName = info.label;
              }
            }
          }
        })
        if (this.pageType === 'edit' || this.pageType === 'detail') {
          if (this.sourceTypeList && this.sourceTypeList.length > 0) {
            let info = this.sourceTypeList.find((item) => item.value == this.model.sourceType);
            if (!!info) {
              this.model.sourceTypeName = info.label;
            }
          }
          if (this.statusList && this.statusList.length > 0) {
            let info = this.statusList.find((item) => item.value == this.model.status);
            if (!!info) {
              this.model.statusName = info.label;
            }
          }
          if (this.stateList && this.stateList.length > 0) {
            let info = this.stateList.find((item) => item.value == this.model.state);
            if (!!info) {
              this.model.stateName = info.label;
            }
          }
          /* if (this.termServiceList && this.termServiceList.length > 0) {
            let info = this.termServiceList.find((item) => item.value == this.model.termService);
            if (!!info) {
              this.model.termServiceName = info.label;
            }
          } */
          if (!!this.model.projectId) {
            getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.projectId}`).then((res) => {
              if (res.success) {
                this.departList = [];
                let treeList = res.result;
                this.departList = [...treeList].map((v) => ({
                  ...v,
                  value: v.id,
                  label: v.departName,
                }))
                let info = this.departList.find((item) => item.value == this.model.departId);
                if (!!info) {
                  this.model.departName = info.label;
                }
              }
            })
          }
          if (!!this.model.departId) {
            getAction(`/sys/sysDepart/getUsersByDepartId?id=${this.model.departId}`).then((res) => {
              if (res.success) {
                this.userList = [];
                let treeList = res.result;
                this.userList = [...treeList].map((v) => ({
                  ...v,
                  value: v.id,
                  label: v.realname,
                }))
                let info = this.userList.find((item) => item.value == this.model.userId);
                if (!!info) {
                  this.model.userName = info.label;
                }
              }
            })
          }

        }
      },
      add(type, record) {
        this.visible = true;
        this.pageType = type;
        this.init();
      },
      edit(type, record) {
        this.visible = true;
        this.pageType = type;
        this.model.id = record.id;
        this.init(record.id);
      },
      init(id) {
        if (this.pageType === 'add') {
          this.model = {
            projectName: '',
            projectId: '',
            categoryOneName: '',
            categoryOneId: '',
            typeName: '',
            type: '',
            name: '',
            model: '',
            factory: '',
            power: '',
            makeDate: '',
            weight: '',
            addProjectName: '',
            addProjectId: '',
            addDepartName: '',
            addDepartId: '',
            address: '',
            teamGroupName: '',
            teamGroup: '',
            supplierName: '',
            supplier: '',
            approachTime: '',
            phone: '',
            sourceTypeName: '',
            sourceType: '',
            purchaseDate: '',
            statusName: '',
            status: '',
            termService: '',
            stateName: '',
            state: '',
            preserverProjectName: '',
            preserverProjectId: '',
            departName: '',
            departId: '',
            userName: '',
            userId: '',
            mark: '',
            appraisement: '',
            isLargeScaleEquipment: '0',
            isSafetyDevice: '0',
            isSpecialEquipment: '0',
            safetyDevice: '',
            accessoryDevice: '',
            specialEquipmentOperationUrl: '',
            specialEquipmentUrl: '',
            ecoMarkUrl: '',
            remark: '',
            picture: '',
            number: '',
          }
        }
        if (this.pageType === 'add' || this.pageType === 'edit') {
          this.validatorRules = {
            projectName: [{
              required: true,
              message: '归属项目不能为空,请选择~',
              trigger: 'change'
            }],
            typeName: [{
              required: true,
              message: '设备类型不能为空,请选择~',
              trigger: 'change'
            }],
            name: [{
              required: true,
              message: '设备名称不能为空,请输入~',
              trigger: 'blur'
            }],
            model: [{
              required: true,
              message: '规格型号不能为空,请输入~',
              trigger: 'blur'
            }],
            number: [{
              required: true,
              message: '设备编号不能为空,请输入~',
              trigger: 'blur'
            }],
            address: [{
              required: true,
              message: '设备位置不能为空,请选择~',
              trigger: 'change'
            }],
            sourceTypeName: [{
              required: true,
              message: '来源类型不能为空,请选择~',
              trigger: 'change'
            }],
            statusName: [{
              required: true,
              message: '设备状态不能为空,请选择~',
              trigger: 'change'
            }],
            stateName: [{
              required: true,
              message: '使用状态不能为空,请选择~',
              trigger: 'change'
            }],
            departName: [{
              required: true,
              message: '保管人不能为空,请选择~',
              trigger: 'change'
            }],
            isLargeScaleEquipment: [{
              required: true,
              message: '是否大型设备不能为空,请选择~',
              trigger: 'change'
            }],
            isSafetyDevice: [{
              required: true,
              message: '是否安全装置是否有效不能为空,请选择~',
              trigger: 'change'
            }],
            isSpecialEquipment: [{
              required: true,
              message: '是否特种设备不能为空,请选择~',
              trigger: 'change'
            }],
          }
        }
        if (this.pageType === 'edit' || this.pageType === 'detail') {
          //供应商列表
          getAction(`/equipment/wtEquipment/queryById?id=${id}`).then((res) => {
            if (res.success) {
              this.model = res.result;
              this.Dictionary();
            }
          })
        } else if (this.pageType === 'add') {
          this.Dictionary();
        }

      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const _self = this;
        if (this.pageType === 'add' || this.pageType === 'edit') {
          if (!this.model.projectName) {
            _self.$message.warning('归属项目不能为空,请选择');
            return;
          }
          if ((!this.model.categoryOneName) || (!this.model.typeName)) {
            _self.$message.warning('设备类型不能为空,请选择');
            return;
          }
          if (!this.model.name) {
            _self.$message.warning('设备名称不能为空,请输入');
            return;
          }
          if (!this.model.model) {
            _self.$message.warning('规格型号不能为空,请输入');
            return;
          }
          if (!this.model.number) {
            _self.$message.warning('设备编号不能为空,请输入');
            return;
          }
          if ((!this.model.addProjectName) || (!this.model.addDepartName) || (!this.model.address)) {
            _self.$message.warning('设备位置不能为空,请选择');
            return;
          }
          if (!this.model.sourceTypeName) {
            _self.$message.warning('来源类型不能为空,请选择');
            return;
          }
          if (!this.model.statusName) {
            _self.$message.warning('设备状态不能为空,请选择');
            return;
          }
          if (!this.model.stateName) {
            _self.$message.warning('使用状态不能为空,请选择');
            return;
          }
          if ((!this.model.preserverProjectName) || (!this.model.departName) || (!this.model.userName)) {
            _self.$message.warning('保管人不能为空,请选择');
            return;
          }
          if (!this.model.isLargeScaleEquipment) {
            _self.$message.warning('是否大型设备不能不能为空,请选择');
            return;
          }
          if (!this.model.isSafetyDevice) {
            _self.$message.warning('是否安全装置是否有效不能为空,请选择');
            return;
          }
          if (!this.model.isSpecialEquipment) {
            _self.$message.warning('是否特种设备不能为空,请选择');
            return;
          }
        }
        let params = {};
        let httpurl = ''
        if (_self.pageType === 'add') {
          httpurl = _self.url.add;
          _self.model.countersignTemplateId = this.countersignTemplateId;
          params = _self.model;
        } else if (_self.pageType === 'edit') {
          httpurl = _self.url.edit;
          params = _self.model;
        }
        // 触发表单验证
        _self.confirmLoading = true;
        console.log('提交表单', params)
        // debugger
        httpAction(httpurl, params, 'POST')
          .then((res) => {
            if (res.success) {
              _self.$message.success(res.message)
              _self.submitCallback();
            } else {
              _self.$message.warning(res.message)
            }
          })
          .finally(() => {
            _self.confirmLoading = false
          })
      },
      submitCallback() {
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel() {
        this.close()
      }
    }
  }
</script>
<style scoped>
  .scroll-view {
    overflow: auto;
    overflow-y: scroll;
  }
</style>